import _mergeJSXProps from "@vue/babel-helper-vue-jsx-merge-props";
import {createNamespace} from '../../utils';
import {inherit} from '../../utils/functional';
import {BORDER_BOTTOM} from '../../utils/constant'; // Types

var _createNamespace = createNamespace('sku-header'),
    createComponent = _createNamespace[0],
    bem = _createNamespace[1];

function getSkuImg(sku, selectedSku) {
    var img;
    sku.tree.some(function (item) {
        var id = selectedSku[item.k_s];

        if (id && item.v) {
            var matchedSku = item.v.filter(function (skuValue) {
                return skuValue.id === id;
            })[0] || {};
            img = matchedSku.previewImgUrl || matchedSku.imgUrl || matchedSku.img_url;
            return img;
        }

        return false;
    });
    return img;
}

function SkuHeader(h, props, slots, ctx) {
    var sku = props.sku,
        goods = props.goods,
        skuEventBus = props.skuEventBus,
        selectedSku = props.selectedSku;
    var goodsImg = getSkuImg(sku, selectedSku) || goods.picture;

    var previewImage = function previewImage() {
        skuEventBus.$emit('sku:previewImage', goodsImg);
    };

    return h("div", _mergeJSXProps([{
        "class": [bem(), BORDER_BOTTOM]
    }, inherit(ctx)]), [h("div", {
        "class": bem('img-wrap'),
        "on": {
            "click": previewImage
        }
    }, [h("img", {
        "attrs": {
            "src": goodsImg
        }
    })]), h("div", {
        "class": bem('goods-info')
    }, [slots.default && slots.default()])]);
}

SkuHeader.props = {
    sku: Object,
    goods: Object,
    skuEventBus: Object,
    selectedSku: Object
};
export default createComponent(SkuHeader);